package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("PurchaseadditiveController")
public class PurchaseadditiveController extends CmiEmaController {

	@RequestMapping("/listPurchaseadditives")
	public ModelAndView listPurchaseadditives(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = purchaseadditivePaginationService.updatePagination(request, pagination);
		Set<Purchaseadditive> purchaseadditives = purchaseadditivePaginationService
				.getPaginationPurchaseadditives(pagination);

		mav.addObject("purchaseadditives", purchaseadditives);
		mav.addObject("entityName", "purchaseadditive");
		mav.addObject("pagination", pagination);
		mav.setViewName("purchaseadditive/listPurchaseadditives");
		return mav;
	}

	public String listPurchaseadditives() {
		return "redirect:/listPurchaseadditives";
	}

	@RequestMapping("/viewPurchaseadditive")
	public ModelAndView viewPurchaseadditive(@RequestParam Integer purchaseadditiveId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("purchaseadditive", purchaseadditiveDAO.findPurchaseadditiveByPrimaryKey(purchaseadditiveId));
		mav.setViewName("purchaseadditive/viewPurchaseadditive");
		return mav;
	}

	@RequestMapping(value = "/confirmDeletePurchaseadditives")
	public ModelAndView confirmDeletePurchaseadditives(
			@RequestParam(value = "deletePurchaseadditivesIds", required = true) Set<Integer> deletePurchaseadditivesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Purchaseadditive> deletePurchaseadditives = purchaseadditiveDAO
				.findPurchaseadditivesByIds(deletePurchaseadditivesIds);
		Set<Purchaseadditive> referencedPurchaseadditives = purchaseadditiveService
				.loadReferencePurchaseadditives(deletePurchaseadditives);
		boolean hasReferenced;
		if (referencedPurchaseadditives.size() == 0) {
			mav.addObject("deleteEntitys", deletePurchaseadditives);
			hasReferenced = false;
		} else {
			deletePurchaseadditives.removeAll(referencedPurchaseadditives);
			mav.addObject("referencedEntitys", referencedPurchaseadditives);
			mav.addObject("deleteEntitys", deletePurchaseadditives);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Purchaseadditive");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listPurchaseadditives");
		return mav;
	}

	@RequestMapping("/deletePurchaseadditive")
	public String deletePurchaseadditive(@RequestParam Integer purchaseadditiveId) {
		Purchaseadditive purchaseadditive = purchaseadditiveDAO.findPurchaseadditiveByPrimaryKey(purchaseadditiveId);
		purchaseadditiveService.deletePurchaseadditive(purchaseadditive);
		return "forward:/listPurchaseadditives";
	}

	@RequestMapping("/confirmDeletePurchaseadditive")
	public ModelAndView confirmDeletePurchaseadditive(@RequestParam Integer purchaseadditiveId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("purchaseadditive", purchaseadditiveDAO.findPurchaseadditiveByPrimaryKey(purchaseadditiveId));

		mav.setViewName("purchaseadditive/deletePurchaseadditive");

		return mav;
	}

	@RequestMapping("/deletePurchaseadditives")
	public String deletePurchaseadditives(
			@RequestParam(value = "deletePurchaseadditivesId", required = true) Set<Integer> deletePurchaseadditivesId) {
		purchaseadditiveService.deletePurchaseadditives(deletePurchaseadditivesId);
		return "forward:/listPurchaseadditives";
	}

	@RequestMapping("/editPurchaseadditive")
	public ModelAndView editPurchaseadditive(@RequestParam Integer purchaseadditiveId) {

		ModelAndView mav = new ModelAndView();

		Purchaseadditive purchaseadditive = purchaseadditiveDAO.findPurchaseadditiveByPrimaryKey(purchaseadditiveId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("purchaseadditive", purchaseadditive);

		// 把所有的引用purchaseadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(purchaseadditiveService.loadPurchaseadditiveObjectReferencedSet(purchaseadditive));

		// 将编辑页面purchaseadditive添加到mav中
		mav.setViewName("purchaseadditive/editPurchaseadditive");
		return mav;
	}

	@RequestMapping("/newPurchaseadditive")
	public ModelAndView newPurchaseadditive() {
		ModelAndView mav = new ModelAndView();
		Purchaseadditive purchaseadditive = new Purchaseadditive();

		mav.addObject("purchaseadditive", purchaseadditive);

		// 把所有的引用purchaseadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(purchaseadditiveService.loadPurchaseadditiveObjectReferencedSet(purchaseadditive));

		mav.addObject("newFlag", true);
		mav.setViewName("purchaseadditive/editPurchaseadditive");

		return mav;
	}

	@RequestMapping("/savePurchaseadditive")
	public ModelAndView savePurchaseadditive(@ModelAttribute Purchaseadditive purchaseadditive) {

		ModelAndView mav = new ModelAndView();

		Additive additiveTemp = purchaseadditive.getAdditive();
		Additive additive = additiveTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(additiveDAO.findAdditiveById(additiveTemp.getId()));
		Producer producerTemp = purchaseadditive.getProducer();
		Producer producer = producerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(producerDAO.findProducerById(producerTemp.getId()));
		Supplier supplierTemp = purchaseadditive.getSupplier();
		Supplier supplier = supplierTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(supplierDAO.findSupplierById(supplierTemp.getId()));
		Unit unitTemp = purchaseadditive.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Purchaseadditive existingPurchaseadditive = CmiHibernateProxyUtil
				.deproxy(purchaseadditiveDAO.findPurchaseadditiveByPhysicalPrimaryKey(producer.getId(),
						supplier.getId(), additive.getId(), purchaseadditive.getDate()));

		purchaseadditive.setAdditive(additive);
		purchaseadditive.setProducer(producer);
		purchaseadditive.setSupplier(supplier);
		purchaseadditive.setUnit(unit);
		// 新建记录
		if (purchaseadditive.getId() == null) {
			if (purchaseadditive.equals(existingPurchaseadditive)) {

				mav.addAllObjects(purchaseadditiveService.loadPurchaseadditiveClassReferencedSet(purchaseadditive));
				// mav = editContinue(mav, purchaseadditive);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("purchaseadditive/editPurchaseadditive");
			} else {
				purchaseadditiveService.savePurchaseadditive(purchaseadditive);
				mav.setViewName("forward:/listPurchaseadditives");
			}
		}
		// 编辑记录
		else {
			Purchaseadditive originPurchaseadditive = CmiHibernateProxyUtil
					.deproxy(purchaseadditiveDAO.findPurchaseadditiveByPrimaryKey(purchaseadditive.getId()));
			if (!purchaseadditive.equals(originPurchaseadditive) && purchaseadditive.equals(existingPurchaseadditive)) {
				mav.addAllObjects(purchaseadditiveService.loadPurchaseadditiveClassReferencedSet(purchaseadditive));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("purchaseadditive/editPurchaseadditive");
			} else {
				purchaseadditiveService.savePurchaseadditive(purchaseadditive);
				mav.setViewName("forward:/listPurchaseadditives");
			}
		}
		return mav;
	}

	@RequestMapping("/purchaseadditiveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
